# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter

import json
import time

from selenium import webdriver
import time

from urllib.parse import urlparse
from urllib.request import urlopen


class MoocCoursePipeline:
    def process_item(self, item, spider):
        file_name = 'data/' + time.strftime('%Y-%m-%d', time.localtime())
        with open(file_name + '-imooc-data', 'a') as f:
            f.write(json.dumps(dict(item)) + '\n')
            f.close()
        return item


class ImoocCourseSaleScreenshot:
    def process_item(self, item, spider):
        driver = webdriver.PhantomJS()
        driver.get(item['course_url'])
        # driver.maximize_window()
        time.sleep(1)
        driver.save_screenshot('course_sale_screenshot/' + item['course_sale_screenshot'] + '.png')
        driver.close()

        file_name = time.strftime('%Y-%M-%d', time.localtime())
        with open('data/' + file_name + '-data', 'a') as f:
            f.write(json.dumps(dict(item)) + '\n')
            f.close()
        return item


class DownloadImagePipeline:
    def process_item(self, item, spider):
        file_name = 'course_images/imooc/' + urlparse(item['course_img_url']).path.split('/')[-1]

        data = urlopen(item['course_img_url']).read()
        with open(file_name, 'wb') as img:
            img.write(data)
            img.close()
        time.sleep(0.5)
        return item
